<!-- 新闻公告 -->
<template>
    <div
        class="newNotice"
        v-loading="loading"
        element-loading-text="正在加载..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(9, 9, 16, 1)"
    >
        <div v-if="!loading">
            <!-- 公司新闻 -->
            <yn-com-comTitle name="公司新闻"></yn-com-comTitle>
            <yn-com-comList
                title="公司新闻"
                :status="true"
                :data="newData"
                :total="newTotal"
                :infoType="newInfoType"
                @page="onPage"
            ></yn-com-comList>
            <!-- 行业新闻 -->
            <yn-com-comTitle name="行业新闻" class="mt15"></yn-com-comTitle>
            <yn-com-comList
                title="行业新闻"
                :data="tradeData"
                :total="tradeTotal"
                :infoType="tradeInfoType"
                @page="onPage"
            ></yn-com-comList>
            <!-- 公司公告 -->
            <yn-com-comTitle name="公司公告" class="mt15"></yn-com-comTitle>
            <yn-com-comList
                title="公司公告"
                :data="newNoticeData"
                :total="noticeTotal"
                infoType="2"
                @page="onPage"
            ></yn-com-comList>
        </div>
    </div>
</template>

<script>
import {corpNew, getNew, corpNotice} from "../script/api";

//数据格式转化----数据处理
const handle = (arr, ops) => {
    if (arr.length != 0) {
        let len = Math.ceil(arr.length / 2);
        let res = [];
        let data = [];
        for (let i = 1; i <= len; i++) {
            let tr = i * 2;
            res = [];
            data.push(res);
            for (let j = 1; j <= arr.length; j++) {
                if (j > tr - 2 && j <= tr) {
                    const idx = arr[j - 1];
                    idx._infoType = ops;
                    idx._title = idx.titleMain ? idx.titleMain : idx.title;
                    idx._negaPosiMark =
                        idx.negaPosiMark || idx.negaPosiMark == 0
                            ? ["中", "负", "正"][idx.negaPosiMark]
                            : "";
                    idx._color =
                        idx.negaPosiMark || idx.negaPosiMark == 0
                            ? ["tableSignWhite", "tableSignGreen", "tableSignRed"][
                                idx.negaPosiMark
                                ]
                            : "";
                    res.push(idx);
                }
            }
        }
        return data;
    }
};
export default {
    data() {
        return {
            newData: [], //公司新闻
            newTotal: 0,
            newInfoType: "1",
            tradeData: [], //行业新闻
            tradeTotal: 0,
            tradeInfoType: "3",
            newNoticeData: [], //公司公告
            noticeTotal: 0,
            noticeInfoType: "2",
            loading: true,
            pageNum: 1,
        };
    },
    components: {},
    computed: {},
    mounted() {
        this.newGetData();
        this.tradeGetData();
        this.noticeGetData();
    },
    methods: {
        async newGetData() {
            //公司新闻
            const newRes = await corpNew({
                infoType: this.newInfoType,
                pageNum: this.pageNum,
            });
            console.log("newRes", newRes.data.totalElements);
            if (newRes.status == 1) {
                this.loading = false;
                this.newData = newRes.data.content && newRes.data.content.length != 0 ? handle(newRes.data.content, this.newInfoType) : [];
                this.newTotal = newRes.data.totalElements;
                console.log(this.newTotal, "this.newTotal");
                // this.newData = handle(newRes.list, this.newInfoType);
                // this.newTotal = newRes.total;
            }
        },
        async tradeGetData() {
            //行业新闻
            const trade = await getNew({
                infoType: this.tradeInfoType,
                pageNum: this.pageNum,
            });
            console.log("trade", trade.data.totalElements);
            if (trade.status == 1) {
                this.tradeData = handle(trade.data.content, this.tradeInfoType);
                this.tradeTotal = trade.data.totalElements;
            }
            // this.tradeData = handle(trade.list, this.tradeInfoType);
            // this.tradeTotal = trade.total;
        },
        async noticeGetData() {
            //公司公告
            const notice = await corpNotice({
                infoType: this.noticeInfoType,
                pageNum: this.pageNum,
            });

            if (notice.status == 1) {
                this.newNoticeData = handle(notice.data.list, this.noticeInfoType);
                this.noticeTotal = notice.data.totalNum;
            }
            // this.newNoticeData = handle(notice.list, this.noticeInfoType);
            // this.noticeTotal = notice.total;
        },
        //分页
        async onPage(ops) {
            console.log(ops, "ops");
            console.log(ops.infoType, "infoType");
            // const res = await getNew({
            //   infoType: ops.infoType,
            //   pageNum: ops.page,
            // }); //公司新闻
            this.pageNum = ops.page;

            if (ops.infoType == 1) {
                console.log("1");
                this.newGetData();
                // this.newData = handle(res.data.content, this.newInfoType);
                // this.newTotal = res.data.totalElements;
            } //公司公告
            if (ops.infoType == 2) {
                console.log("公告");
                this.noticeGetData();
                // this.newNoticeData = handle(res.data.content, this.noticeInfoType);
                // this.noticeTotal = res.data.totalElements;
            } //行业新闻
            if (ops.infoType == 3) {
                console.log("1");
                this.tradeGetData();
                // this.tradeData = handle(res.data.content, this.tradeInfoType);
                // this.tradeTotal = res.data.totalElements;
            }
        },
    },
};
</script>
<style lang="scss" scoped></style>
